<?php
/**
 * Created by PhpStorm.
 * User: Digvijay
 * Date: 8/29/14
 * Time: 7:10 PM
 */

class Users extends MY_Controller {
    public function client() {
        $data = array("pageTitle" => "Client Management");
        $this->load->view("admin/client-view", $data);
    }

    public function getClients($id = null) {
        if (!$this->input->is_ajax_request()) {
            show_404();
            return;
        }
        $response = new Response();
        try {
            $clients = null;
            if ($id != null && $id != "") {

            } else {
                $clients = $this->getAgentsByType(Agency::CLIENT_AGENT);
            }
            $response->setData($clients);
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
            log_message("error", $e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response, JSON_PRETTY_PRINT);
    }

    public function getUnregisteredClients($id = null) {
        if (!$this->input->is_ajax_request()) {
            show_404();
            return;
        }
        $response = new Response();
        try {
            $clients = null;
            if ($id != null && $id != "") {

            } else {
                $clients = $this->getAgentsByType(Agency::CLIENT_ANONYMOUS);
            }
            $response->setData($clients);
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
            log_message("error", $e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response, JSON_PRETTY_PRINT);
    }


    public function changeClientStatus($id = null, $type = null) {
        if (!$this->input->is_ajax_request()) {
            show_404();
            return;
        }
        $response = new Response();
        try {
            $agency = $this->findById("Agency", $id);
            $emailMessage = "";
            $emailSubject = "";
            if ($type == Agency::STATUS_APPROVED) {
                $agency->setStatus(Agency::STATUS_APPROVED);
                $emailSubject = "Account Approved at AnmolCards";
                $emailMessage = "Your account at  <a href='http://www.anmolcards.net/'>AnmolCards.net</a> has been approved, Please <a href='http://www.anmolcards.net/index.php/client/login'>Login</a> and start shopping";
            } else if ($type == Agency::STATUS_BLOCKED) {
                $agency->setStatus(Agency::STATUS_BLOCKED);
                $emailSubject = "Account Suspended at AnmolCards";
                $emailMessage = "Your account at <a href='http://www.anmolcards.net/'>AnmolCards.net</a> has been blocked, Please contact us for more details";
            }
            $this->save($agency);
            $message = "Hello " . $agency->getFirstName() . " " . $agency->getLastName() . ",<br /><br/>";
            $message .= $emailMessage;
            $message .= "<br/><br/> Thank You";
            $this->sendMail($agency->getEmail(), $emailSubject, $message);
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
            log_message("error", $e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response, JSON_PRETTY_PRINT);
    }

    public function updateClientDiscount($id = null, $discount = null) {
        if (!$this->input->is_ajax_request()) {
            show_404();
            return;
        }
        $response = new Response();
        try {
            if ($id != null && $id != "" && $discount != null & $discount != "") {
                $agency = $this->findById("Agency", $id);
                if ($agency != null) {
                    $agency->setDiscount($discount);
                }
                $this->save($agency);
            }
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
            log_message("error", $e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response, JSON_PRETTY_PRINT);
    }
} 